package com.sy.musicapp.ui.home

import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.sy.musicapp.model.*
import com.sy.musicapp.ui.theme.MusicAppTheme

/**
 * 主页
 * @author SY
 * @since 2022-01-24 23:17
 **/
@ExperimentalComposeUiApi
@ExperimentalAnimationApi
@Composable
fun HomeScreen(
    navController: NavController,
    searchModel: SearchViewModel,
    userModel: UserInfoViewModel,
    playlistModel: PlaylistViewModel,
    musicModel: MusicViewModel
) {
    Column(
        verticalArrangement = Arrangement.spacedBy(20.dp),
        modifier = Modifier
            .padding(bottom = 55.dp)
            .verticalScroll(rememberScrollState())
    ) {
        val hotKey by searchModel.hotKey.collectAsState()
        val userInfo by userModel.userInfo.collectAsState()
        val banners by userModel.banners.collectAsState()
        val recomMusics by userModel.recomMusics.collectAsState()
        val playlists by playlistModel.playlists.collectAsState()
        playlistModel.getPlaylist(userInfo.userId)
        SearchBar(hotKey) {
            searchModel.searchKey(it)
            navController.navigate(Screen.Search.route + "/$it")
        }
        BannerImg(banners)
        UserProfileCard(userInfo) {
            userModel.logout {
                if (it) {
                    userModel.resetAll()
                    playlistModel.resetAll()
                    musicModel.resetAll()
                    searchModel.resetAll()
                    navController.navigate(Screen.Login.route)
                }
            }
        }
        PlaylistCard(
            playlists,
            onSelectItem = {
                playlistModel.getPlaylistMusic(it)
                navController.navigate(Screen.Playlist.route + "/${playlistModel.playlists.value[it].name}")
            },
            addPlaylist = {},
            delPlaylist = {}
        )
        RecommendCard(recomMusics, musicModel)
    }
}

@ExperimentalComposeUiApi
@ExperimentalAnimationApi
@Preview(showSystemUi = true)
@Composable
fun HomeScreenPrev() {
    MusicAppTheme {
        HomeScreen(
            rememberNavController(),
            SearchViewModel(),
            UserInfoViewModel(),
            PlaylistViewModel(),
            MusicViewModel()
        )
    }
}

@ExperimentalComposeUiApi
@ExperimentalAnimationApi
@Preview(showSystemUi = true)
@Composable
fun HomeScreenPrevDark() {
    MusicAppTheme(darkTheme = true) {
        HomeScreen(
            rememberNavController(),
            SearchViewModel(),
            UserInfoViewModel(),
            PlaylistViewModel(),
            MusicViewModel()
        )
    }
}